package com.younion.business.impl;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.CollectionUtils;

import com.younion.business.ICardEntityService;
import com.younion.common.paginator.Paginator;
import com.younion.common.util.ImageFileUtil;
import com.younion.dao.CardEntityDAO;
import com.younion.vo.API_CardEntityVO;
import com.younion.vo.CardEntityVO;

import cpcns.component.common.base.BaseException;
import cpcns.jhsks.util.MapCollectionUtils;

 
public class CardEntityServiceImpl implements ICardEntityService {

	protected static final Log logger = LogFactory
			.getLog(CardEntityServiceImpl.class);

	private CardEntityDAO cardEntityDAO;

	public Collection getAll() throws Exception {
		return cardEntityDAO.selectAll();
	}

	public CardEntityVO getById(String id) throws Exception {
		if (StringUtils.isEmpty(id) || Integer.parseInt(id) < 1)
			return null;
		try {
			Map paramMap = new HashMap();
			paramMap.put("id", new Integer(id));
			Object obj = cardEntityDAO.selectById(paramMap);
			return obj == null ? null : (CardEntityVO) obj;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CardEntityServiceImpl.class.getName()
					+ " >> public CardEntityVO get(String id) error occur:", e);
		}
	}

	public Collection getByMap(Map paramMap) throws BaseException {
		if (CollectionUtils.isEmpty(paramMap)) {
			return null;
		}
		try {
			return cardEntityDAO.selectByMap(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CardEntityServiceImpl.class.getName()
					+ " >> public Collection getByMap(Map map) error occur:", e);
		}
	}

	public int getCountByMap(Map paramMap) throws BaseException {
		try {
			return cardEntityDAO.selectCountByMap(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					CardEntityServiceImpl.class.getName()
							+ " >> public Collection getCountByMap(Map map) error occur:",
					e);
		}
	}

	public List get(Map paramMap, Paginator paginator) throws Exception {
		if (MapCollectionUtils.isEmpty(paramMap)) {
			return null;
		}
		try {
			Map map = new HashMap();
			map.put("paramMap", paramMap);
			return cardEntityDAO.selectForPage(map, paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					CardEntityServiceImpl.class.getName()
							+ " >> public Page (Map paramMap, Paginator paginator) error occur:",
					e);
		}
	}

	public CardEntityVO save(CardEntityVO voObject) throws Exception {
		if (voObject == null)
			return null;
		try {
			Object id = null;
			id = voObject.getId();
			Map paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils
					.describe(voObject));
			if (id != null) {
				cardEntityDAO.updateByMap(paramMap);
				return voObject;
			} else {
				return (CardEntityVO) cardEntityDAO.insertByVo(voObject);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					CardEntityServiceImpl.class.getName()
							+ " >> public CardEntityVO save(CardEntityVO voObject) error occur:",
					e);
		}
	}

	public Collection save(Collection voList) throws Exception {
		if (CollectionUtils.isEmpty(voList))
			return null;
		try {
			Iterator it = voList.iterator();
			while (it.hasNext()) {
				CardEntityVO data = (CardEntityVO) it.next();
				data = save(data);
			}
			return voList;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					CardEntityServiceImpl.class.getName()
							+ " >> public Collection save(Collection voList) error occur:",
					e);
		}
	}

	public boolean remove(List ids) throws Exception {
		if (CollectionUtils.isEmpty(ids))
			return false;
		try {
			cardEntityDAO.deleteByIds(ids);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CardEntityServiceImpl.class.getName()
					+ " >> public boolean remove(List ids) error occur:", e);
		}
	}

	public boolean removeByMap(Map paramMap) throws Exception {
		if (CollectionUtils.isEmpty(paramMap))
			return false;
		try {
			cardEntityDAO.deleteByMap(paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					CardEntityServiceImpl.class.getName()
							+ " >> public boolean removeByMap(Map paramMap) error occur:",
					e);
		}
	}

	/**
	 * @param service
	 */
	public void setCardEntityDAO(CardEntityDAO cardEntityDAO) {
		this.cardEntityDAO = cardEntityDAO;
	}

	public int saveReturnKey(CardEntityVO voObject) throws Exception {
		if (voObject == null)
			return 0;
		try {
			if (voObject.getId() != null) {
				if (cardEntityDAO.updateReturnKey(voObject) > 0) {
					return voObject.getId();
				} else {
					return 0;
				}
			} else {
				return cardEntityDAO.saveReturnKey(voObject);
			}

		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					RechargeRuleServiceImpl.class.getName()
							+ " >> public int saveReturnKey(RechargeRuleVO rechargeRuleVO) error occur:",
					e);
		}
	}

	public List selectAllByChannel(Map paramMap) throws Exception {
		try {
			return cardEntityDAO.selectAllByChannel(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					RechargeRuleServiceImpl.class.getName()
							+ " >> public List selectAllByChannel(int channel, Map paramMap, int page,int pageSize) error occur:",
					e);
		}
	}
	public List selectCustomerByCard(Map paramMap) throws Exception{
		try {
			return cardEntityDAO.selectCustomerByCard(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					RechargeRuleServiceImpl.class.getName()
							+ " >> public List selectAllByChannel(int channel, Map paramMap, int page,int pageSize) error occur:",
					e);
		}
	}
	
	public List selectMyFavorites(Map paramMap) throws Exception{
		try {
			return cardEntityDAO.selectMyFavorites(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					RechargeRuleServiceImpl.class.getName()
							+ " >> public List selectMyFavorites error occur:",
					e);
		}
	}
	
	public boolean updateFavourAmountByMap(Map paramMap) throws Exception {
		if (CollectionUtils.isEmpty(paramMap))
			return false;
		try {
			cardEntityDAO.updateFavourAmountByMap(paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					CardCountInitServiceImpl.class.getName()
							+ " >> public boolean updateFavourAmountByMap(Map paramMap) error occur:",
					e);
		}
	}
	
	public boolean updateSendCardAmountByMap(Map paramMap) throws Exception {
		if (CollectionUtils.isEmpty(paramMap))
			return false;
		try {
			cardEntityDAO.updateSendCardAmountByMap(paramMap);
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(
					CardCountInitServiceImpl.class.getName()
							+ " >> public boolean updateSendCardAmountByMap(Map paramMap) error occur:",
					e);
		}
	}

	public CardEntityVO selectByCustomerAndRand(int customerId) throws Exception {
		try {
			return this.cardEntityDAO.selectByCustomerAndRand(customerId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CardCountInitServiceImpl.class.getName()
							+ " >> public CardEntityVO selectByCustomerAndRound(int customerId) error occur:", e);
		}
	}
	
	public API_CardEntityVO selectApiCardEntityByMap(Map paramMap)throws Exception{
		try {
			return this.cardEntityDAO.selectApiCardEntityByMap(paramMap);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new BaseException(CardCountInitServiceImpl.class.getName()
							+ " >> public API_CardEntityVO selectApiCardEntityByMap(Map paramMap) error occur:", e);
		}
	}
	
	public boolean deleteImgByCustomerIds(List<String> customerIds , String path) throws BaseException{
		if(CollectionUtils.isEmpty(customerIds)){
			return false;
		}
		try{
			Map map = new HashMap();
			for(String customerId : customerIds){
				map.put("customerId", customerId);
				List<CardEntityVO> cardList = (List<CardEntityVO>) getByMap(map);
				for(CardEntityVO card : cardList){
					File directory = new File(path + File.separator
							+ card.getId());
					String fileNames = card.getNonactivatedPic()+","+card.getActivatedPic();
					ImageFileUtil.deleteImage(directory, fileNames, true);	
				}
				map.clear();
			}
			return true;
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			throw new BaseException(CardCountInitServiceImpl.class.getName()+" >> public boolean deleteImgByCustomerIds(List customerIds , String path) error occur:",e);
			
		}
	}
}